package com.skivingcloud.admin.utils;

import com.skivingcloud.admin.security.SecurityUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author hushouquan
 */
public class UserUtil{
    private static final Logger log = LoggerFactory.getLogger(UserUtil.class);
    public static SecurityUser getCurrentUser(){
        try{
            UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            //details里面可能存放了当前登录用户的详细信息，也可以通过cast后拿到
            return (SecurityUser) authenticationToken.getPrincipal();
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return new SecurityUser();
        }
    }
}
